package com.lanou3g.k12.web.controller;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lanou3g.k12.entity.CourseCategory;
import com.lanou3g.k12.service.CourseCategoryService;
import com.lanou3g.k12.utils.K12Result;
import com.lanou3g.k12.utils.K12ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class CourseCategoryController {
    @Autowired
    private CourseCategoryService courseCategoryService;
    @GetMapping("/course/categories")
    //查询搜索框里的内容和展现课程分类列表
    public K12Result list(String searchText,@RequestParam(value = "page",defaultValue = "1") Integer page,
                          @RequestParam(value = "size",defaultValue = "30") Integer size){//searchText前台搜索框中的内容
        Page<Object> objects = PageHelper.startPage(page, size);
        if(searchText!=null&& !searchText.trim().equals("")){  //trim()去掉首尾的空串
            //查询搜索框里的内容
            courseCategoryService.search(searchText);
        }else {
            //展现课程分类列表
            courseCategoryService.findAll();
        }
        try {
            return K12ResultUtils.ok(objects);
        }catch (Exception e){
            e.printStackTrace();
            return K12ResultUtils.error(500);
        }
    }

    //获取多个id删除课程分类
    @DeleteMapping("/course/categories/{ids}")
    public K12Result delete(@PathVariable Long[] ids){
        courseCategoryService.delete(ids);
        try {
            return K12ResultUtils.ok("ok");
        }catch (Exception e){
            e.printStackTrace();
            return K12ResultUtils.error(500);
        }
    }

    //添加记录到课程分类
    @PostMapping("/course/category")
    public K12Result insert(CourseCategory courseCategory){
        courseCategoryService.insert(courseCategory);
        try {
            return K12ResultUtils.ok(courseCategory);
        }catch (Exception e){
            e.printStackTrace();
            return K12ResultUtils.error(500);
        }
    }

    //根据id查询某一个课程分类
    @GetMapping("/course/category/{id}")
    public K12Result selectById(@PathVariable Long id){
        CourseCategory courseCategory = courseCategoryService.selectById(id);
        try {
            return K12ResultUtils.ok(courseCategory);
        }catch (Exception e){
            e.printStackTrace();
            return K12ResultUtils.error(500);
        }
    }

    //根据id修改课程列表
    @PutMapping("/course/category/{id}")
    public K12Result update(@PathVariable Long id,CourseCategory courseCategory){
        courseCategoryService.update(id,courseCategory);
        try {
            return K12ResultUtils.ok();
        }catch (Exception e){
            e.printStackTrace();
            return K12ResultUtils.error(500);
        }
    }

    //根据id删除课程列表
    @DeleteMapping("/course/category/{id}")
    public K12Result deleteById(@PathVariable Long id){
        courseCategoryService.deleteById(id);
        try {
            return K12ResultUtils.ok();
        }catch (Exception e){
            e.printStackTrace();
            return K12ResultUtils.error(500);
        }
    }
}
